﻿#include "FITKNastranAdaptorProperty.h"
#include "FITKNastranBDFIOTool.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKNasSectionInfoPSHELL.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKNasSectionInfoPBAR.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"

namespace Nastran
{
    bool FITKNastranAdaptorProperty::adaptR()
    {
        if (!_reader) return false;
        //后退一行
        _reader->backLine();
        //获取读取工具和网格数据
        FITKNastranBDFIOReaderTool* readTool = _reader->getReaderTool();
        if (!readTool) return false;
        //获取数据
        QStringList data = readTool->readNastranBDFLineSpliter();
        if (data.isEmpty()) return false;
        QString propType = data.at(0).toUpper();
        //读入属性
        bool readOK = false;
        if (propType.startsWith("PSHELL"))
            readOK = this->readPSHELL(data);
        else if (propType.startsWith("PBAR"))
            readOK = this->readPBAR(data);
        return readOK;
    }

    bool FITKNastranAdaptorProperty::adaptW()
    {
        Interface::FITKAbaAbastractSection *section = dynamic_cast<Interface::FITKAbaAbastractSection*>(_dataObj);
        if (!section || !_writer) return false;
        bool writeOK = false;
        if (section->getSectionType() == Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous)
            writeOK = this->writePSHELL();
        else if (section->getSectionType() == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam)
            writeOK = this->writePBAR();
        return writeOK;
    }

    bool FITKNastranAdaptorProperty::readPSHELL(QStringList data)
    {
        Interface::FITKAbaSectionShellHomogeneous *section = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(_dataObj);
        if (data.size() < 2 || !section || !_reader) return false;
        //设置名称
        section->setDataObjectName("PSHELL-1");
        FITKNastranBDFIOReaderTool* readTool = _reader->getReaderTool();
        if (!readTool) return false;
        bool ok = false;
        //pID Integer>0
        int pID = data.at(1).trimmed().toInt(&ok);
        if (!ok) return false;
        //膜材料ID Integer>0 or blank
        int m1ID = -1;
        if (data.size() >= 3 && !data.at(2).trimmed().isEmpty())
            m1ID = data.at(2).trimmed().toInt(&ok);
        //厚度 Real or blank
        double thickness = -1.0;
        if (data.size() >= 4 && !data.at(3).trimmed().isEmpty())
            thickness = readTool->stringToDouble(data.at(3), ok);
        //弯曲材料ID Integer>=-1 or blank
        int m2ID = -1;
        if (data.size() >= 5 && !data.at(4).trimmed().isEmpty())
            m2ID = data.at(4).trimmed().toInt(&ok);
        //弯曲力矩惯性比 Real>0.0; Default=1.0
        double bendingMoment = 1.0;
        if (data.size() >= 6 && !data.at(5).trimmed().isEmpty())
            bendingMoment = readTool->stringToDouble(data.at(5), ok);
        //横向剪贴材料ID (Integer>0 or blank)
        int m3ID = -1;
        if (data.size() >= 7 && !data.at(6).trimmed().isEmpty())
            m3ID = data.at(6).trimmed().toInt(&ok);
        //横向剪贴厚度比 (Real>0.0; Default=.833333)
        double shearThickness = 0.833333;
        if (data.size() >= 8 && !data.at(7).trimmed().isEmpty())
            shearThickness = readTool->stringToDouble(data.at(7), ok);
        //质量 Real
        double mass = 0.0;
        if (data.size() >= 9 && !data.at(8).trimmed().isEmpty())
            mass = readTool->stringToDouble(data.at(8), ok);
        //Z1 Real or blank
        double z1Distance = 0.0;
        if (data.size() >= 10 && !data.at(9).trimmed().isEmpty())
            mass = readTool->stringToDouble(data.at(9), ok);
        //Z2 Real or blank
        double z2Distance = 0.0;
        if (data.size() >= 11 && !data.at(10).trimmed().isEmpty())
            mass = readTool->stringToDouble(data.at(10), ok);
        //膜片弯曲耦合材料ID Integer>0 or blank
        int m4ID = -1;
        if (data.size() >= 12 && !data.at(11).trimmed().isEmpty())
            m4ID = data.at(11).trimmed().toInt(&ok);
        //截面信息
        section->setThickness(thickness);
        Interface::FITKNasSectionInfoPSHELL* pshellInfo = new Interface::FITKNasSectionInfoPSHELL;
        pshellInfo->setBendingMoment(bendingMoment);
        pshellInfo->setNonStructuralMass(mass);
        pshellInfo->setTransverseShear(shearThickness);
        pshellInfo->setFiberDistanceZ1(z1Distance);
        pshellInfo->setFiberDistanceZ2(z2Distance);
        section->setSectionInfo(pshellInfo);
        //设置数据到映射表
        readTool->insertDataMapping(FITKNastranBDFIOReaderTool::ReadMappingType::ReadPIDSection, pID, section->getDataObjectID());
        readTool->insertPIDMID(pID, QList<int>() << m1ID << m2ID << m3ID << m4ID);
        return true;
    }

    bool FITKNastranAdaptorProperty::writePSHELL()
    {
        Interface::FITKAbaSectionShellHomogeneous *section = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(_dataObj);
        if (!section || !_writer) return false;
        Interface::FITKNasSectionInfoPSHELL* pshellInfo = dynamic_cast<Interface::FITKNasSectionInfoPSHELL*>(section->getSectionInfo());
        if (!pshellInfo) return false;
        //获取写出工具
        FITKNastranBDFIOWriterTool* writeTool = _writer->getWriterTool();
        if (!writeTool) return false;
        //获取数据
        bool ok = false;
        QStringList data;
        data.append("PSHELL");
        data.append(QString::number(section->getDataObjectID()));
        data.append(QString::number(section->getMaterialID(0)));
        data.append(writeTool->doubleToString(section->getThickness(), ok));
        data.append(QString::number(section->getMaterialID(1)));
        data.append(writeTool->doubleToString(pshellInfo->getBendingMoment(), ok));
        data.append(QString::number(section->getMaterialID(2)));
        data.append(writeTool->doubleToString(pshellInfo->getNonStructuralMass(), ok));
        data.append(writeTool->doubleToString(pshellInfo->getTransverseShear(), ok));
        data.append(writeTool->doubleToString(pshellInfo->getFiberDistanceZ1(), ok));
        data.append(writeTool->doubleToString(pshellInfo->getFiberDistanceZ2(), ok));
        //写出数据
        writeTool->writeData(data);
        return true;
    }

    bool FITKNastranAdaptorProperty::readPBAR(QStringList data)
    {
        Interface::FITKAbaSectionBeam *section = dynamic_cast<Interface::FITKAbaSectionBeam*>(_dataObj);
        if (data.size() < 2 || !section || !_reader) return false;
        //设置名称
        section->setDataObjectName("PBAR-1");
        FITKNastranBDFIOReaderTool* readTool = _reader->getReaderTool();
        if (!readTool) return false;
        bool ok = false;
        //pID Integer>0
        int pID = data.at(1).trimmed().toInt(&ok);
        if (!ok) return false;
        //膜材料ID Integer>0
        int m1ID = -1;
        if (data.size() >= 3 && !data.at(2).trimmed().isEmpty())
            m1ID = data.at(2).trimmed().toInt(&ok);
        //横杆截面的面积
        double area = 0.0;
        if (data.size() >= 4 && !data.at(3).trimmed().isEmpty())
            area = readTool->stringToDouble(data.at(3), ok);
        //惯性矩
        double i1 = 0.0, i2 = 0.0, i12 = 0.0;
        if (data.size() >= 5 && !data.at(4).trimmed().isEmpty())
            i1 = readTool->stringToDouble(data.at(4), ok);
        if (data.size() >= 6 && !data.at(5).trimmed().isEmpty())
            i2 = readTool->stringToDouble(data.at(5), ok);
        if (data.size() >= 20 && !data.at(19).trimmed().isEmpty())
            i12 = readTool->stringToDouble(data.at(19), ok);
        //扭转常数
        double torsionalConstant = 0.0;
        if (data.size() >= 7 && !data.at(6).trimmed().isEmpty())
            torsionalConstant = readTool->stringToDouble(data.at(6), ok);
        //质量
        double mass = 0.0;
        if (data.size() >= 8 && !data.at(7).trimmed().isEmpty())
            mass = readTool->stringToDouble(data.at(7), ok);
        //应力
        double c1 = 0.0, c2 = 0.0, d1 = 0.0, d2 = 0.0, e1 = 0.0, e2 = 0.0, f1 = 0.0, f2 = 0.0;
        if (data.size() >= 10 && !data.at(9).trimmed().isEmpty())
            c1 = readTool->stringToDouble(data.at(9), ok);
        if (data.size() >= 11 && !data.at(10).trimmed().isEmpty())
            c2 = readTool->stringToDouble(data.at(10), ok);
        if (data.size() >= 12 && !data.at(11).trimmed().isEmpty())
            d1 = readTool->stringToDouble(data.at(11), ok);
        if (data.size() >= 13 && !data.at(12).trimmed().isEmpty())
            d2 = readTool->stringToDouble(data.at(12), ok);
        if (data.size() >= 14 && !data.at(13).trimmed().isEmpty())
            e1 = readTool->stringToDouble(data.at(13), ok);
        if (data.size() >= 15 && !data.at(14).trimmed().isEmpty())
            e2 = readTool->stringToDouble(data.at(14), ok);
        if (data.size() >= 16 && !data.at(15).trimmed().isEmpty())
            f1 = readTool->stringToDouble(data.at(15), ok);
        if (data.size() >= 17 && !data.at(16).trimmed().isEmpty())
            f2 = readTool->stringToDouble(data.at(16), ok);
        //剪贴面积系数
        double k1 = 0.0, k2 = 0.0;
        if (data.size() >= 18 && !data.at(17).trimmed().isEmpty())
            k1 = readTool->stringToDouble(data.at(17), ok);
        if (data.size() >= 19 && !data.at(18).trimmed().isEmpty())
            k2 = readTool->stringToDouble(data.at(18), ok);
        //截面信息
        Interface::FITKNasSectionInfoPBAR* pbarInfo = new Interface::FITKNasSectionInfoPBAR;
        pbarInfo->setArea(area);
        pbarInfo->setInertiaAreaMomentsI1(i1);
        pbarInfo->setInertiaAreaMomentsI2(i2);
        pbarInfo->setInertiaAreaMomentsI12(i12);
        pbarInfo->setTorsionalConstant(torsionalConstant);
        pbarInfo->setNonstructuralMass(mass);
        pbarInfo->setStressRecoveryCoeffC1(c1);
        pbarInfo->setStressRecoveryCoeffC2(c2);
        pbarInfo->setStressRecoveryCoeffD1(d1);
        pbarInfo->setStressRecoveryCoeffD2(d2);
        pbarInfo->setStressRecoveryCoeffE1(e1);
        pbarInfo->setStressRecoveryCoeffE2(e2);
        pbarInfo->setStressRecoveryCoeffF1(f1);
        pbarInfo->setStressRecoveryCoeffF2(f2);
        pbarInfo->setAreaShearFactorK1(k1);
        pbarInfo->setAreaShearFactorK2(k2);
        section->setSectionInfo(pbarInfo);
        //设置数据到映射表
        readTool->insertDataMapping(FITKNastranBDFIOReaderTool::ReadMappingType::ReadPIDSection, pID, section->getDataObjectID());
        readTool->insertPIDMID(pID, QList<int>() << m1ID);
        return true;
    }

    bool FITKNastranAdaptorProperty::writePBAR()
    {
        Interface::FITKAbaSectionBeam *section = dynamic_cast<Interface::FITKAbaSectionBeam*>(_dataObj);
        if (!section || !_writer) return false;
        Interface::FITKNasSectionInfoPBAR* pbarInfo = dynamic_cast<Interface::FITKNasSectionInfoPBAR*>(section->getSectionInfo());
        if (!pbarInfo) return false;
        //获取写出工具
        FITKNastranBDFIOWriterTool* writeTool = _writer->getWriterTool();
        if (!writeTool) return false;
        //获取数据
        bool ok = false;
        QStringList data;
        data.append("PBAR");
        data.append(QString::number(section->getDataObjectID()));
        data.append(QString::number(section->getMaterialID(0)));
        data.append(writeTool->doubleToString(pbarInfo->getArea(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getInertiaAreaMomentsI1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getInertiaAreaMomentsI2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getTorsionalConstant(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getNonstructuralMass(), ok));
        data.append("");
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffC1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffC2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffD1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffD2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffE1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffE2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffF1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getStressRecoveryCoeffF2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getAreaShearFactorK1(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getAreaShearFactorK2(), ok));
        data.append(writeTool->doubleToString(pbarInfo->getInertiaAreaMomentsI12(), ok));
        //写出数据
        writeTool->writeData(data);
        return true;
    }
}
